package com.gml.jdk.annotation.table;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 构建表创建语句
 * @author gml
 * @version V1.0
 * @Package com.gml.test.annotation.table
 * @date 2020/9/9 23:08
 */
public class TableCreator {

    public static void main(String[] args) throws ClassNotFoundException {
        System.out.println(createTableSql("com.gml.test.annotation.table.Member"));
    }

    public static String createTableSql(String className) throws ClassNotFoundException {
        Class<?> clazz = Class.forName(className);

        DBTable dbTable = clazz.getAnnotation(DBTable.class);
        if (dbTable == null){
            System.out.println("NO DBTable annotations");
            return null;
        }

        String tableName = dbTable.name();
        if (tableName.length() < 1){
            tableName = clazz.getName().toUpperCase();
        }

        List<String> columnDefs = new ArrayList<>();

        for (Field field : clazz.getDeclaredFields()) {
            String columnName = null;
            //获取字段上的注解
            Annotation[] annotations = field.getDeclaredAnnotations();
            if (annotations.length < 1){
                continue;
            }

            //判断注解类型
            if (annotations[0] instanceof SQLInteger){
                SQLInteger sqlInteger = (SQLInteger) annotations[0];
                //获取字段对应表中列名，如果没有使用字段名代替
                if (sqlInteger.name().length() < 1){
                    columnName = field.getName().toUpperCase();
                }else {
                    columnName = sqlInteger.name();
                }
                columnDefs.add(columnName + " INT " + getConstraints(sqlInteger.constraints()));
            }

            if (annotations[0] instanceof  SQLString){
                SQLString sqlString = (SQLString) annotations[0];
                if (sqlString.name().length() < 1){
                    columnName = field.getName().toUpperCase();
                }else {
                    columnName = sqlString.name();
                }

                columnDefs.add(columnName + " VARCHAR（" + sqlString.value() +")" + getConstraints(sqlString.constraints()));
            }

        }

        //构建建表语句
        StringBuilder createCommond = new StringBuilder(" CREATE TABLE " + tableName +" (");
        for (String columnDef : columnDefs){
            createCommond.append("\n " + columnDef +",");
        }

        String tableCreate = createCommond.substring(
                0, createCommond.length() - 1) + ");";

        return tableCreate;

    }

    public static String getConstraints(Constraints cons){
        String constraints = "";
        if (!cons.allowNull()){
            constraints += " NOT NULL ";
        }
        if (cons.primaryKey()){
            constraints += " PRIMARY KEY ";
        }
        if (cons.unique()){
            constraints += " UNIQUE ";
        }
        return constraints;
    }

}
